<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xami
 * Date: 12-9-9
 * Time: 上午12:05
 * To change this template use File | Settings | File Templates.
 */

class JUserModel extends CActiveRecord
{
    public $verifyPassword;
    public $verifyCode;

    public $user_type;
    public $user_title;

    public $username;
    public $password;
    public $email;
    public $activkey;
    public $createtime;
    public $lastvisit;
    public $superuser;
    public $status;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }


    public function tableName()
    {
        return '{{profiles}}';
    }

    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => '用户ID',
            'password' => '密码',
            'verifyPassword' => '重复密码',
            'email' => '邮箱',
            'activkey' => 'Activkey',
            'createtime' => 'Createtime',
            'lastvisit' => 'Lastvisit',
            'superuser' => 'Superuser',
            'status' => 'Status',
            'nickname' => ($this->user_type=='个人会员') ? '姓名' : '法定单位名称',
            'idcard' => ($this->user_type=='个人会员') ? '身份证号' : '组织机构代码',
            'sex' => '性别',
            'birthday' => ($this->user_type=='个人会员') ? '出生日期' : '单位成立时间',
            'place' => '籍贯',
            'nation' => '民族',
            'marry' => '婚姻状况',
            'address' => '家庭地址',
            'contact' => '通信地址',
            'post' => '邮政编码',
            'phone' => '联系电话',
            'im' => ($this->user_type=='个人会员') ? 'Q Q' : '联络QQ',
            'politics' => '政治面貌',
            'jobtitle' => '职 称',
            'job' => '职 务',
            'blog' => ($this->user_type=='个人会员') ? '博客地址' : '单位网址',
            'pphoto' => '个人照片',
            'highest_education' => '最高学历',
            'graduation_date' => '毕业时间',
            'school' => '就读院校',
            'department' => '系别',
            'specialities' => '所学专业',
            'research_directions' => '主要学术研究方向',
            'work_experience' => '工作经历',
            'scientific_result' => '研究成果',
            'achievement' => ($this->user_title=='晋学会员') ? '已经获取（包括）院系级以上的奖项' : '获取的成就,荣誉及奖励',
            'social_position' => ($this->user_title=='晋学会员') ? '担任何种校外兼职' : '担任的社会职务' ,
            'n_second_school_major' => '就读第二学历高校名称及专业',
            'n_computer_skill' => ($this->user_title=='晋学会员') ? '加盟学会希望获得的计算机技能服务' : '计算机相关技能',
            'n_computer_achievement' => '在计算机技能或学术方面获取的重大奖项',
            'n_skill_mark' => '技能标签',
            'n_work' => '工作经历信息',
            'n_activity' => '希望参加的专项活动（最多三项）',
            's_admission_date' => '入学时间',
            's_studentid' => '学生证号',
            's_school_position' => '担任何种校内职务',
            'c_legal_person' => '法人代表',
            'c_hr_manager' => '行政(人事)负责人',
            'c_contacter_tel' => '联络人及移动电话',
            'c_introduce_photo' => '组织机构形象照',
            'c_id_photo' => '组织机构代码证件照',
            'c_introduce' => '单位简况及其他关键信息描述',
            'c_board_members' => '董事会成员',
            'type_pg' => 'Type Pg',
            'type_p_vasn' => 'Type P Vasn',
            'type_g_pebg' => 'Type G Pebg',
            'open' => '公开',
            'c_email' => '单位对外电子邮箱',
        );
    }


    public function rules() {
        if($this->user_type=='个人会员'){
            $rules = array(
                array('username, nickname, email, marry, contact, post, phone, im, politics, pphoto, highest_education, graduation_date, school, department, specialities', 'required'),
    //            array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
    //            array('password', 'length', 'max'=>20, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
                array('email', 'email'),
//                array('c_email', 'unique', 'message' => '此邮箱地址已经存在'),
    //            array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
    //            array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
    //            array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect.")),
    //            array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
    //
    //            array('user_id, sex, birthday, marry, politics, highest_education, type_pg, type_p_vasn, type_g_pebg, open', 'numerical', 'integerOnly'=>true),
    //            array('nickname, address, contact, blog, pphoto, n_second_school_major, n_skill_mark, c_introduce_photo, c_id_photo, n_activity, c_board_members', 'length', 'max'=>255),
    //            array('idcard, place, phone, jobtitle, job, s_studentid, c_legal_person, c_hr_manager', 'length', 'max'=>32),
    //            array('nation, school, department, specialities, social_position, s_school_position, c_contacter_tel, c_email', 'length', 'max'=>64),
    //            array('post, graduation_date, s_admission_date', 'length', 'max'=>8),
    //            array('im', 'length', 'max'=>12),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
            );
        }elseif($this->user_type=='团体会员'){
            $rules = array(
                array('username, nickname, email, contact, post, blog, phone, im, c_hr_manager, c_contacter_tel, c_email, c_id_photo, c_introduce', 'required'),
                array('email, c_email', 'email'),
//                array('c_email', 'unique', 'message' => '此邮箱地址已经存在'),
            );
        }
//        if (isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
//            return $rules;
//        else
//            array_push($rules,array('verifyCode', 'captcha', 'allowEmpty'=>!UserModule::doCaptcha('registration')));
        return $rules;
    }


    public function upload($file_name, $destPath, $label){
        Yii::import('application.extensions.upload.Upload');
        $Upload = new Upload((isset($file_name) ? $file_name : null));
        $Upload->jpeg_quality  = 100;
        $Upload->no_script     = false;
        $Upload->image_ratio   = false;
        $Upload->image_resize  = false;
        $Upload->file_max_size = 1024*1024;

        // some vars
        $newName  = md5(rand(1,100).date('YmdHis').rand(1,1000));

        if ($Upload->uploaded) {
            $Upload->file_new_name_body = $newName;
            $Upload->process($destPath);

            // if was processed
            if ($Upload->processed) {
                return $Upload->file_dst_name;
                // write image filename on table
            } else {
                throw new CHttpException(400,$Upload->error);
            }
        } else {
            throw new CHttpException(400,'请选择'.$label);
        }
    }
}